package com.Txunda.fxw.ui.activity.law;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.ui.activity.other.LeaveMessageAty;
import com.Txunda.fxw.ui.activity.other.NoticeAty;
import com.Txunda.fxw.ui.adapter.MyRecyclerAdapter;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.callback.MyItemClickListener;
import com.Txunda.fxw.ui.http.AboutUs;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.Txunda.fxw.view.RoundedImageView;
import com.bumptech.glide.Glide;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 法院立案
 *
 * @author Administrator
 */
public class CourtCaseAty extends BaseAty {

    private Office office;
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.law_lv)
    private PullToRefreshListView law_lv;
    private List<Map<String, String>> list_data;
    private MyAdapter adapter;
    private String from;
    @ViewInject(R.id.img_logo)
    private RoundedImageView img_logo;
    private AboutUs aboutUs;

    // 屏幕宽度
    private int mScreenWidth = 0;
    private XCSlideView mSlideViewRight;
    private Context mContext;

    private RecyclerView main_recycler;
    private MyRecyclerAdapter recyclerAdapter;
    private MyItemClickListener mItemClickListener;

    private TextView tv_comment;
    private TextView tv_weibo;
    private TextView tv_link;
    private ArrayList<Map<String, String>> recycler_list_data;
    private int p = 1;

    private List<Map<String, String>> list_more_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from = getIntent().getExtras().getString("from");

        showProgressDialog();
        if ("1".equals(from)) {
            aty_title3_tv_center.setText("法院立案须知");
            office.articleList("1", p, this);
        } else if ("4".equals(from)) {
            aty_title3_tv_center.setText("法律文书格式");
            office.articleList("2", p, this);
        } else if ("5".equals(from)) {
            aty_title3_tv_center.setText("法律法规");
            office.articleList("3", p, this);
        }

        law_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                p = 1;
                showProgressDialog();
                if ("1".equals(from)) {
                    office.articleList("1", p, CourtCaseAty.this);
                } else if ("4".equals(from)) {
                    office.articleList("2", p, CourtCaseAty.this);
                } else if ("5".equals(from)) {
                    office.articleList("3", p, CourtCaseAty.this);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                p++;
                showProgressDialog();
                if ("1".equals(from)) {
                    office.articleList("1", p, CourtCaseAty.this);
                } else if ("4".equals(from)) {
                    office.articleList("2", p, CourtCaseAty.this);
                } else if ("5".equals(from)) {
                    office.articleList("3", p, CourtCaseAty.this);
                }
            }
        });
        law_lv.onRefreshComplete();

        law_lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("a_id", list_data.get(position - 1).get("a_id"));
                bundle.putString("title", list_data.get(position - 1).get("title"));
                startActivity(Civil_Procedure_LawAty.class, bundle);
            }
        });

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_court_case;
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("introduce")) { // 介绍信息
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                String microblog = data.get("microblog"); // 微博
                data.get("microblog_url");
                final String url = data.get("url"); // 网址
                recycler_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
                recyclerAdapter.setList(recycler_list_data);
                main_recycler.setAdapter(recyclerAdapter);
                tv_link.setText(url);

                tv_weibo.setText(microblog);

                tv_link.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);

                    }
                });
            }
        }
        if (requestUrl.contains("articleList")) {

            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                if (!map.get("data").equals("null") && map.get("data") != null) {
                    if (p == 1) {
                        list_data.clear();
                        list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        law_lv.setAdapter(adapter);

                    } else {

                        list_more_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        list_data.addAll(list_more_data);
                        adapter.notifyDataSetChanged();
                    }
                }

            }
            law_lv.onRefreshComplete();
        }
    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        super.onError(requestUrl, error);
        // removeProgressDialog();
        law_lv.onRefreshComplete();
    }

    @Override
    protected void initialized() {
        img_logo.setVisibility(View.GONE);
        aboutUs = new AboutUs();
        recycler_list_data = new ArrayList<>();
        String logo = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(img_logo);
        office = new Office();
        list_data = new ArrayList<>();
        list_more_data = new ArrayList<>();
        adapter = new MyAdapter();

        mContext = this;
        mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(CourtCaseAty.this, XCSlideView.Positon.RIGHT);
        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_slideview, null);
        TextView tv_notice = menuViewRight.findViewById(R.id.tv_notice);
        main_recycler = menuViewRight.findViewById(R.id.main_recycler);

        tv_weibo = menuViewRight.findViewById(R.id.tv_weibo);
        tv_comment = menuViewRight.findViewById(R.id.tv_comment);
        tv_link = menuViewRight.findViewById(R.id.tv_link);
        mSlideViewRight.setMenuView(CourtCaseAty.this, menuViewRight);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);
        // 公告
        tv_notice.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(NoticeAty.class, null);
            }
        });

        tv_comment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(LeaveMessageAty.class, null);
            }
        });
        tv_weibo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showTips(R.drawable.error, "敬请期待");
            }
        });

        // 设置LinearLayoutManager
        main_recycler
                .setLayoutManager(new LinearLayoutManager(CourtCaseAty.this, LinearLayoutManager.HORIZONTAL, false));
        main_recycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        main_recycler.setHasFixedSize(true);

        recyclerAdapter = new MyRecyclerAdapter(CourtCaseAty.this);

    }

    @Override
    protected void requestData() {

    }

    @Override
    @OnClick({R.id.img_logo})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.img_logo: // logo图标
                if (!mSlideViewRight.isShow())
                    mSlideViewRight.show();

                showProgressDialog();
                aboutUs.introduce(this);
                break;

            default:
                break;
        }
    }

    /**
     * 法院立案Adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {

        private MyViewHodler vh;

        @Override
        public int getCount() {
            int ret = 0;
            if (list_data != null) {
                ret = list_data.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list_data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(CourtCaseAty.this).inflate(R.layout.item_law_aty, null);
                vh = new MyViewHodler();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHodler) convertView.getTag();
            }
            vh.tv_title.setText(list_data.get(position).get("title"));
            vh.tv_subtitle.setText(list_data.get(position).get("content"));
            return convertView;
        }

        class MyViewHodler {
            @ViewInject(R.id.tv_title)
            private TextView tv_title;
            @ViewInject(R.id.tv_subtitle)
            private TextView tv_subtitle;
        }

    }

}
